/*****************************************************
*                                                                          
*               COPYRIGHT (c) 2015-2017 Broadlink Corporation              
*                         All Rights Reserved                              
*                                                                          
* The source code contained or described herein and all documents          
* related to the source code ("Material") are owned by Broadlink         
* Corporation or its licensors.  Title to the Material remains             
* with Broadlink Corporation or its suppliers and licensors.               
*                                                                          
* The Material is protected by worldwide copyright and trade secret        
* laws and treaty provisions. No part of the Material may be used,         
* copied, reproduced, modified, published, uploaded, posted, transmitted,  
* distributed, or disclosed in any way except in accordance with the       
* applicable license agreement.                                            
*                                                                          
* No license under any patent, copyright, trade secret or other            
* intellectual property right is granted to or conferred upon you by       
* disclosure or delivery of the Materials, either expressly, by            
* implication, inducement, estoppel, except in accordance with the         
* applicable license agreement.                                            
*                                                                          
* Unless otherwise agreed by Broadlink in writing, you may not remove or   
* alter this notice or any other notice embedded in Materials by Broadlink 
* or Broadlink's suppliers or licensors in any way.                        
*                                                                          
** Author: shengda.huang
** Date: 2018-07-17
*
*****************************************************/

/***** Include files ********************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/***** Defines **************************************/

/***** Define structure *****************************/

typedef struct 
{
	unsigned int 	size;
	void 			*ptr;
} mem_mgr_t;

/***** Define constant ******************************/

/***** Define global ********************************/

int total_size, max_size, max_once;
mem_mgr_t mem_mgr[1024];

/***** Define Prototype of functions ****************/

/**
 * @brief
 * @param  None
 * @retval None
 */
static mem_mgr_t *find_free_mode(void)
{
	int i;

	for (i=0; i<1024; i++)
	{
		if (mem_mgr[i].ptr == (void *)0)
			return &mem_mgr[i];
	}
	return (void *)0;
}

static mem_mgr_t *find_exist_node(void *ptr)
{
	int i;

	for (i=0; i<1024; i++)
	{
		if (mem_mgr[i].ptr == ptr)
			return &mem_mgr[i];
	}
	return (void *)0;
}

void user_mem_init(void)
{
	total_size = max_size = max_once = 0;
	memset((unsigned char *)mem_mgr, 0x0, sizeof(mem_mgr_t)*1024);
}

void *user_malloc(unsigned int size)
{
	static int cnt = 0;

	mem_mgr_t *mem_ptr = (void *)0;
	void *tar_ptr = (void *)0;

	mem_ptr = find_free_mode();
	if (mem_ptr == (void *)0)
		return (void *)0;

	tar_ptr = malloc(size);
	if (tar_ptr == NULL)
		return NULL;

	mem_ptr->size = size;
	mem_ptr->ptr = tar_ptr;
	total_size += size;

	if (size > max_once)
	{
		max_once = size;
		// printf("%s %d: current_size %d total_size %d, max_size %d, max_once %d\n", __func__, cnt++, size, total_size, max_size, max_once);
	}

	if (total_size > max_size)
	{
		max_size = total_size;
		// printf("%s %d: current_size %d total_size %d, max_size %d, max_once %d\n", __func__, cnt++, size, total_size, max_size, max_once);
	}

	
	return tar_ptr;
}

void user_free(void *ptr)
{
	mem_mgr_t *mem_ptr = (void *)0;

	if (ptr == NULL)
		return ;

	mem_ptr = find_exist_node(ptr);
	if (mem_ptr == (void *)0)
	{
		free(ptr);
		return ;
	}

	total_size -= mem_ptr->size;
	mem_ptr->ptr = (void *)0;
	free(ptr);
	// printf("%s: total_size %d\n", __func__, total_size);
}

/* End of file */



